package com.example.JavaWebApi.core.ip;

import javax.servlet.http.HttpServletRequest;
import java.net.InetAddress;
import java.net.UnknownHostException;

public class IpHelper {
    // 获得用户IP
    public static String GetUserIp(HttpServletRequest request) {
        String ip = request.getHeader("x-forwarded-for");
        if (ip != null && ip.length() != 0 && !"unknown".equalsIgnoreCase(ip)) {
            // 多次反向代理后会有多个ip值，第一个ip才是真实ip
            if (ip.indexOf(",") != -1) {
                ip = ip.split(",")[0];
            }
        }
        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
            ip = request.getHeader("Proxy-Client-IP");
        }
        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
            ip = request.getHeader("WL-Proxy-Client-IP");
        }
        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
            ip = request.getHeader("HTTP_CLIENT_IP");
        }
        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
            ip = request.getHeader("HTTP_X_FORWARDED_FOR");
        }
        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
            ip = request.getHeader("X-Real-IP");
        }
        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
            ip = request.getRemoteAddr();
        }
        if (ip.equals("0:0:0:0:0:0:0:1")) {
            ip = "1.1.1.1";
        }
        return ip;
    }

    public static String GetIpAddress() throws UnknownHostException {
        InetAddress addr = InetAddress.getLocalHost();
        String hostname = addr.getHostName();
        return hostname;
    }


    /// 提取开启代理/cdn服务后的客户端真实IP
    public static String GetTrueIP(HttpServletRequest request) {
        String ip = request.getHeader("x-forwarded-for");
        if (ip != null && ip.length() != 0 && !"unknown".equalsIgnoreCase(ip)) {
            // 多次反向代理后会有多个ip值，第一个ip才是真实ip
            if (ip.indexOf(",") != -1) {
                ip = ip.split(",")[0];
            }
        } else {
            String CF_Connecting_IP = request.getHeader("CF-Connecting-IP");
            if (CF_Connecting_IP != null && CF_Connecting_IP.length() != 0 && !"unknown".equalsIgnoreCase(CF_Connecting_IP)) {
                ip = CF_Connecting_IP;
            } else {
                ip = request.getRemoteHost();
            }
        }
        if (ip.equals("0:0:0:0:0:0:0:1")) {
            ip = "127.0.0.1";
        }
        return ip;
    }

    /// 字符串（ip）转数值存储
    public static long IPToLong(String ip) {
        String[] ips = ip.split("\\.");
        return 16777216 * Long.parseLong(ips[0]) + 65536 * Long.parseLong(ips[1]) + 256 * Long.parseLong(ips[2]) + Long.parseLong(ips[3]);
    }

    /// 数值存储转字符串（ip）
    public static String LongToIP(long ip)//返回字符型I，整型转字符型。
    {
        return ((ip / 16777216) % 256) + "." + ((ip / 65536) % 256) + "." + ((ip / 256) % 256) + "." + ((ip) % 256);
    }
}
